package com.gp.testonline.service.impl;

import com.gp.testonline.dao.TeacherMapper;
import com.gp.testonline.eo.TeacherEOPage;
import com.gp.testonline.model.Teacher;
import com.gp.testonline.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/*
 *实现业务及流程接口当中的内容
 */
@Service
public class TeacherServiceImpl implements TeacherService {
    @Autowired
    private TeacherMapper teacherMapper;

    @Override
    public int InsertTeacher(Teacher teacher) {
        int isInsert=0;
        /*
         * 业务判断
         * 教师编号不能重复
         */
        int teacheridCount=teacherMapper.getTeacheridCount(teacher.getTeacherid());
        if (teacheridCount<1){
            //返回值
            isInsert=teacherMapper.insertTeacher(teacher);
        }else{
            isInsert=-1;//有相同教师编号
        }
        return isInsert;
    }

    @Override
    public int UpdateTeacher(Teacher teacher) {
        int isUpdate=0;
        /*
         * 业务判断
         * 教师编号不能重复
         */
        int teacheridCount=teacherMapper.getTeacheridCount(teacher.getTeacherid());
        if (teacheridCount<1){
            //返回值
            isUpdate=teacherMapper.updateTeacher(teacher);
        }else{
            isUpdate=-1;//有相同教师编号
        }
        return isUpdate;
    }

    @Override
    public int DeleteTeacher(int tid) {
        int isDelete=0;
        /*1 没有相关业务流程的判断*/
        try{
            isDelete=teacherMapper.deleteTeacher(tid);
        }catch (Exception e){
            isDelete=-1;
            //打印更新时报错的情况
            System.out.println("删除教师信息时执行sql报错的原因为："+e.getMessage());
        }
        return isDelete;
    }

    @Override
    public List<TeacherEOPage> getTeacherAll(TeacherEOPage teacherEOPage) {
        List<TeacherEOPage> listTeacherEo=teacherMapper.getTeacherAll(teacherEOPage);
        return listTeacherEo;
    }
}
